home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Programming / ModemLink / Examples / TestMLDev.c < prev    next >
C/C++ Source or Header  |  1997-10-25  |  5KB  |  155 lines

  1. /*
  2. ** NAME: TestMLDev.c
  3. ** DESC: A terminal style program to test the modemlink.device.  It requires
  4. **       that the modemlink device be in devs: or in the current directory.
  5. **       It will open the serial.device using the settings saved in
  6. **       serial.prefs.
  7. **
  8. ** AUTHOR:        DATE:       DESCRIPTION:
  9. ** ~~~~~~~~~~~~~~ ~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. ** Mike Veroukis  06 Apr 1997 Created
  11. ** Mike Veroukis  13 Oct 1997 Added extra print statements to help make it
  12. **                            clearer what it's doing.
  13. */
  14.  
  15.  
  16. #include <exec/types.h>
  17. #include <exec/memory.h>
  18. #include <exec/io.h>
  19. #include <devices/serial.h>
  20. #include <utility/tagitem.h>
  21.  
  22. #include <proto/exec.h>
  23. #include <proto/dos.h>
  24. #include <proto/intuition.h>
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28.  
  29. #include <ModemLink/ModemLink.h>
  30. #include <proto/ModemLink.h>
  31. #include "devicestuff.h"
  32.  
  33. struct Library *ModemLinkBase;
  34.  
  35. void main(int argc, char **argv)
  36. {
  37.   struct IOExtLink *LinkWriteIO, *LinkReadIO;
  38.   struct IOExtSer *SerIO;
  39.   struct MsgPort *LinkWriteMP, *LinkReadMP, *SerMP;
  40.   char buf[512];
  41.   int Connect, BusyCount = 0;
  42.  
  43.   printf("Test ModemLink Device -- Let's hope this thing works!!!\n");
  44.  
  45.   if (argc < 3) {
  46.     if (LinkWriteMP = CreateMsgPort()) {
  47.       if (LinkWriteIO = CreateIORequest(LinkWriteMP, sizeof(struct IOExtLink))) {
  48.         if (!(Connect = OpenDevice(MODEMLINKNAME, 0L, (struct IORequest *)LinkWriteIO, 0L))) {
  49.           ModemLinkBase = (struct Library *)LinkWriteIO->IOLink.io_Device;
  50.  
  51.           if (OpenSerialDevice(&SerMP, &SerIO, "serial.device", 0L)) {
  52.  
  53.             if (argc == 2)
  54.               do {
  55.                 if (BusyCount)
  56.                   Delay(150);
  57.  
  58.                 printf("Dialing %s....\n", argv[1]);
  59.  
  60.                 Connect = ML_DialTags(SerIO, argv[1], TAG_DONE);
  61.                 printf("Dialer ReturnCode: %d\n", Connect);
  62.               } while (Connect == MODEM_BUSY && BusyCount++ < 2);
  63.             else {
  64.               printf("Waiting for incomming call...\n");
  65.  
  66.               Connect = ML_AnswerTagList(SerIO, NULL);
  67.             }
  68.  
  69.             printf("Modem ReturnCode: %d\n", Connect);
  70.  
  71.             if (Connect == MODEM_CONNECT) {
  72.               Connect = ML_EstablishTags(LinkWriteIO, SerIO, TAG_DONE);
  73.  
  74.               printf("Establish ReturnCode: %d\n", Connect);
  75.  
  76.               if (Connect == EstErr_OK) {
  77.                 printf("Connected!!!\n\n");
  78.                 printf("Type message and hit return to send\n");
  79.                 printf("Hit return to check for incomming messages\n");
  80.                 printf("Enter '.' and hit return on a new line to quit\n\n");
  81.  
  82.                 if (CloneIO((struct IORequest *)LinkWriteIO, &LinkReadMP, (struct IORequest **)&LinkReadIO)) {
  83.                   LinkReadIO->IOLink.io_Command = CMD_READ;
  84.                   LinkReadIO->IOLink.io_Data = 0;
  85.                   SendIO((struct IORequest *)LinkReadIO);
  86.  
  87.                   while (1) {
  88.                     printf("\n: ");
  89.                     gets(buf);
  90.                     if (buf[0] == '.' && buf[1] == 0)
  91.                       break;
  92.  
  93.                     if (buf[0] > ' ') {
  94.                       printf("Sending: [%s]\n", buf);
  95.  
  96.                       LinkWriteIO->IOLink.io_Command = CMD_WRITE;
  97.                       LinkWriteIO->IOLink.io_Data = &buf;
  98.                       LinkWriteIO->IOLink.io_Length = strlen(buf) + 1;
  99.                       DoIO((struct IORequest *)LinkWriteIO);
  100.                     }
  101.  
  102.                     if (CheckIO((struct IORequest *)LinkReadIO)) {
  103.                       WaitIO((struct IORequest *)LinkReadIO);
  104.                       DisplayBeep(NULL);
  105.  
  106.                       if (!LinkReadIO->IOLink.io_Error) {
  107.                         printf(">> [%s]\n", LinkReadIO->IOLink.io_Data);
  108.  
  109.                         FreeMem(LinkReadIO->IOLink.io_Data, LinkReadIO->IOLink.io_Length);
  110.  
  111.                         LinkReadIO->IOLink.io_Command = CMD_READ;
  112.                         LinkReadIO->IOLink.io_Data = 0;
  113.                         SendIO((struct IORequest *)LinkReadIO);
  114.                       }
  115.                       else
  116.                         printf("io_Error: %X\n", LinkReadIO->IOLink.io_Error);
  117.                     }
  118.  
  119.                     LinkWriteIO->IOLink.io_Command = MLCMD_QUERY;
  120.                     DoIO((struct IORequest *)LinkWriteIO);
  121.                     if (LinkWriteIO->IOLink.io_Error)
  122.                       break;
  123.                   }
  124.  
  125.                   if (!LinkReadIO->IOLink.io_Error) {
  126.                     AbortIO((struct IORequest *)LinkReadIO);
  127.                     if (!CheckIO((struct IORequest *)LinkReadIO))
  128.                       WaitIO((struct IORequest *)LinkReadIO);
  129.                   }
  130.  
  131.                   DeleteIO_MP(LinkReadMP, (struct IORequest *)LinkReadIO);
  132.                 }
  133.  
  134.                 ML_Terminate(LinkWriteIO);
  135.               }
  136.             }
  137.  
  138.             SafeCloseDevice(SerMP, (struct IORequest *)SerIO);
  139.           }
  140.  
  141.           CloseDevice((struct IORequest *)LinkWriteIO);
  142.         }
  143.         else
  144.           printf("ERROR: Could not open modemlink.device\n");
  145.  
  146.         DeleteIORequest((struct IORequest *) LinkWriteIO);
  147.       }
  148.  
  149.       DeleteMsgPort(LinkWriteMP);
  150.     }
  151.   }
  152.   else
  153.     printf("\nUSAGE: TestMLDev <PhoneNumber>\n");
  154. }
  155.